import Vuex from 'vuex'
import axios from 'axios'
import Vue from 'vue'
import article from './modules/article'
import list from './modules/list'
import about from './modules/about'

Vue.use(Vuex);

export default new Vuex.Store({
    modules: {
        article,
        list,
        about
    },
    state: {
        menus: [],
        slides: [],
        // 文章列表
        items: [],

        // 浏览排行
        views: [],

        // 推荐
        recommends: [],

        // 友情连接
        links: [],

        // 我的博客
        blog: []
    },

    mutations: {
        setIndexNavItems: (state, object) => {
            state.indexNavItems = object;
        },
        setSlides: (state, object) => {
            state.slides = object
        },
        setItems: (state, object) => {
            state.items = object
        },
        recommend: (state, object) => {
            state.recommends = object
        },
        view: (state, object) => {
            state.views = object;
        },

        link: (state, object) => {
            state.links = object;
        },

        setBlog(state, blog) {
            state.blog = blog;
        },

        setMenus(state, menus) {
            state.menus = menus;
        }
    },
    actions: {
        // 获取导航
        getMenus({commit}) {
            axios.get('/vue/nav/index', {
                params: {type_id: 1}
            }).then((response) => {
                if (response.data.code === 0) {
                    commit("setMenus", response.data.data.nav_info);
                }
            });
        },

        // 获取幻灯片
        slides({commit}) {
            axios.get("/vue/slides/index", {
                params: {type_id: 1}
            }).then(response => {
                if (response.data.code === 0) {
                    commit("setSlides", response.data.data);
                }
            });
        },

        // 获取文章信息
        articles({commit}, params) {
            let key = params["key"];
            delete params["key"];
            axios.get("/vue/articles/index", {
                params: params
            }).then(response => {
                if (response.data.code === 0) {
                    commit(key, response.data.data)
                }
            })
        },

        // 友情链接
        links({commit}) {
            axios.get("/vue/nav/index", {
                params: {
                    type_id: 4,
                }
            }).then(response => {
                if (response.data.code === 0) {
                    commit("link", response.data.data)
                }
            })
        },

        // 我的博客
        getBlog({commit}) {
            axios.get("/vue/articles/index", {
                params: {
                    limit: 6,
                    recommend: 1,
                }
            }).then(response => {
                if (response.data.code === 0) {
                    commit('setBlog', response.data.data)
                }
            });
        }
    }
});